<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Fanqie;

class UpdateBookPrices extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'books:update-prices';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update book prices to integers';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->info('Updating book prices...');
        
        $books = Fanqie::all();
        
        foreach ($books as $book) {
            // 将价格转换为整数（去掉小数部分）
            $newPrice = intval($book->price);
            $book->price = $newPrice;
            $book->save();
            
            $this->info("Updated book ID {$book->id}: {$book->title} - Price: {$newPrice}");
        }
        
        $this->info('Book prices updated successfully!');
        
        return 0;
    }
}